namespace DbDiploma.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class InitialCreate : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.UserEntities",
                c => new
                    {
                        UserId = c.Int(nullable: false, identity: true),
                        FirstName = c.String(),
                        LastName = c.String(),
                        NickName = c.String(),
                        Password = c.String(nullable: false),
                        VkId = c.String(),
                        FbId = c.String(),
                        TwId = c.String(),
                        Email = c.String(nullable: false),
                        UserEntity_UserId = c.Int(),
                        Role_RoleId = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.UserId)
                .ForeignKey("dbo.UserEntities", t => t.UserEntity_UserId)
                .ForeignKey("dbo.RoleEntities", t => t.Role_RoleId, cascadeDelete: true)
                .Index(t => t.UserEntity_UserId)
                .Index(t => t.Role_RoleId);
            
            CreateTable(
                "dbo.RoleEntities",
                c => new
                    {
                        RoleId = c.Int(nullable: false, identity: true),
                        Role = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.RoleId);
            
            CreateTable(
                "dbo.PermissionEntities",
                c => new
                    {
                        PermissionId = c.Int(nullable: false, identity: true),
                        Permission = c.Int(nullable: false),
                        Role_RoleId = c.Int(),
                        UserEntity_UserId = c.Int(),
                    })
                .PrimaryKey(t => t.PermissionId)
                .ForeignKey("dbo.RoleEntities", t => t.Role_RoleId)
                .ForeignKey("dbo.UserEntities", t => t.UserEntity_UserId)
                .Index(t => t.Role_RoleId)
                .Index(t => t.UserEntity_UserId);
            
            CreateTable(
                "dbo.LikeEntities",
                c => new
                    {
                        LikeId = c.Int(nullable: false, identity: true),
                        Liked = c.DateTime(nullable: false),
                        User_UserId = c.Int(),
                        Event_EventId = c.Int(),
                    })
                .PrimaryKey(t => t.LikeId)
                .ForeignKey("dbo.UserEntities", t => t.User_UserId)
                .ForeignKey("dbo.EventEntities", t => t.Event_EventId)
                .Index(t => t.User_UserId)
                .Index(t => t.Event_EventId);
            
            CreateTable(
                "dbo.EventEntities",
                c => new
                    {
                        EventId = c.Int(nullable: false, identity: true),
                        Title = c.String(nullable: false),
                        Description = c.String(nullable: false),
                        Created = c.DateTime(nullable: false),
                        Begin = c.DateTime(nullable: false),
                        End = c.DateTime(nullable: false),
                        Address = c.String(nullable: false),
                        Latitude = c.Long(nullable: false),
                        Longitude = c.Long(nullable: false),
                        IsApproved = c.Boolean(nullable: false),
                        IsValidated = c.Boolean(nullable: false),
                        IsValidating = c.Boolean(nullable: false),
                        Country_Countryid = c.Int(),
                        City_CityId = c.Int(),
                        Creator_UserId = c.Int(),
                    })
                .PrimaryKey(t => t.EventId)
                .ForeignKey("dbo.CountryEntities", t => t.Country_Countryid)
                .ForeignKey("dbo.CityEntities", t => t.City_CityId)
                .ForeignKey("dbo.UserEntities", t => t.Creator_UserId)
                .Index(t => t.Country_Countryid)
                .Index(t => t.City_CityId)
                .Index(t => t.Creator_UserId);
            
            CreateTable(
                "dbo.CountryEntities",
                c => new
                    {
                        Countryid = c.Int(nullable: false, identity: true),
                        Country = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.Countryid);
            
            CreateTable(
                "dbo.CityEntities",
                c => new
                    {
                        CityId = c.Int(nullable: false, identity: true),
                        City = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.CityId);
            
            CreateTable(
                "dbo.CommentEntities",
                c => new
                    {
                        CommentId = c.Int(nullable: false, identity: true),
                        Text = c.String(nullable: false),
                        Created = c.DateTime(nullable: false),
                        Creator_UserId = c.Int(),
                        EventEntity_EventId = c.Int(),
                    })
                .PrimaryKey(t => t.CommentId)
                .ForeignKey("dbo.UserEntities", t => t.Creator_UserId)
                .ForeignKey("dbo.EventEntities", t => t.EventEntity_EventId)
                .Index(t => t.Creator_UserId)
                .Index(t => t.EventEntity_EventId);
            
            CreateTable(
                "dbo.TagEntities",
                c => new
                    {
                        TagId = c.Int(nullable: false, identity: true),
                        Name = c.String(nullable: false),
                    })
                .PrimaryKey(t => t.TagId);
            
            CreateTable(
                "dbo.FollowerEntities",
                c => new
                    {
                        FollowerId = c.Int(nullable: false, identity: true),
                        FollowState = c.Int(nullable: false),
                        User_UserId = c.Int(),
                        Event_EventId = c.Int(),
                    })
                .PrimaryKey(t => t.FollowerId)
                .ForeignKey("dbo.UserEntities", t => t.User_UserId)
                .ForeignKey("dbo.EventEntities", t => t.Event_EventId)
                .Index(t => t.User_UserId)
                .Index(t => t.Event_EventId);
            
            CreateTable(
                "dbo.SubscriptionEntities",
                c => new
                    {
                        SubscriptionId = c.Int(nullable: false, identity: true),
                        Type = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.SubscriptionId);
            
            CreateTable(
                "dbo.SessionEntities",
                c => new
                    {
                        SessionId = c.Int(nullable: false, identity: true),
                        SessionHashid = c.String(nullable: false),
                        Data = c.String(nullable: false),
                    })
                .PrimaryKey(t => t.SessionId)
                .ForeignKey("dbo.UserEntities", t => t.SessionId)
                .Index(t => t.SessionId);
            
            CreateTable(
                "dbo.LogEntities",
                c => new
                    {
                        LogId = c.Int(nullable: false, identity: true),
                        Url = c.String(nullable: false),
                        Duration = c.Int(nullable: false),
                        IP = c.String(nullable: false),
                        User_UserId = c.Int(),
                        Action_ActionId = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.LogId)
                .ForeignKey("dbo.UserEntities", t => t.User_UserId)
                .ForeignKey("dbo.ActionEntities", t => t.Action_ActionId, cascadeDelete: true)
                .Index(t => t.User_UserId)
                .Index(t => t.Action_ActionId);
            
            CreateTable(
                "dbo.ActionEntities",
                c => new
                    {
                        ActionId = c.Int(nullable: false, identity: true),
                        Action = c.Int(nullable: false),
                        ErrorLevel = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.ActionId);
            
            CreateTable(
                "dbo.CityEntityCountryEntities",
                c => new
                    {
                        CityEntity_CityId = c.Int(nullable: false),
                        CountryEntity_Countryid = c.Int(nullable: false),
                    })
                .PrimaryKey(t => new { t.CityEntity_CityId, t.CountryEntity_Countryid })
                .ForeignKey("dbo.CityEntities", t => t.CityEntity_CityId, cascadeDelete: true)
                .ForeignKey("dbo.CountryEntities", t => t.CountryEntity_Countryid, cascadeDelete: true)
                .Index(t => t.CityEntity_CityId)
                .Index(t => t.CountryEntity_Countryid);
            
            CreateTable(
                "dbo.TagEntityEventEntities",
                c => new
                    {
                        TagEntity_TagId = c.Int(nullable: false),
                        EventEntity_EventId = c.Int(nullable: false),
                    })
                .PrimaryKey(t => new { t.TagEntity_TagId, t.EventEntity_EventId })
                .ForeignKey("dbo.TagEntities", t => t.TagEntity_TagId, cascadeDelete: true)
                .ForeignKey("dbo.EventEntities", t => t.EventEntity_EventId, cascadeDelete: true)
                .Index(t => t.TagEntity_TagId)
                .Index(t => t.EventEntity_EventId);
            
            CreateTable(
                "dbo.SubscriptionEntityUserEntities",
                c => new
                    {
                        SubscriptionEntity_SubscriptionId = c.Int(nullable: false),
                        UserEntity_UserId = c.Int(nullable: false),
                    })
                .PrimaryKey(t => new { t.SubscriptionEntity_SubscriptionId, t.UserEntity_UserId })
                .ForeignKey("dbo.SubscriptionEntities", t => t.SubscriptionEntity_SubscriptionId, cascadeDelete: true)
                .ForeignKey("dbo.UserEntities", t => t.UserEntity_UserId, cascadeDelete: true)
                .Index(t => t.SubscriptionEntity_SubscriptionId)
                .Index(t => t.UserEntity_UserId);
            
        }
        
        public override void Down()
        {
            DropIndex("dbo.SubscriptionEntityUserEntities", new[] { "UserEntity_UserId" });
            DropIndex("dbo.SubscriptionEntityUserEntities", new[] { "SubscriptionEntity_SubscriptionId" });
            DropIndex("dbo.TagEntityEventEntities", new[] { "EventEntity_EventId" });
            DropIndex("dbo.TagEntityEventEntities", new[] { "TagEntity_TagId" });
            DropIndex("dbo.CityEntityCountryEntities", new[] { "CountryEntity_Countryid" });
            DropIndex("dbo.CityEntityCountryEntities", new[] { "CityEntity_CityId" });
            DropIndex("dbo.LogEntities", new[] { "Action_ActionId" });
            DropIndex("dbo.LogEntities", new[] { "User_UserId" });
            DropIndex("dbo.SessionEntities", new[] { "SessionId" });
            DropIndex("dbo.FollowerEntities", new[] { "Event_EventId" });
            DropIndex("dbo.FollowerEntities", new[] { "User_UserId" });
            DropIndex("dbo.CommentEntities", new[] { "EventEntity_EventId" });
            DropIndex("dbo.CommentEntities", new[] { "Creator_UserId" });
            DropIndex("dbo.EventEntities", new[] { "Creator_UserId" });
            DropIndex("dbo.EventEntities", new[] { "City_CityId" });
            DropIndex("dbo.EventEntities", new[] { "Country_Countryid" });
            DropIndex("dbo.LikeEntities", new[] { "Event_EventId" });
            DropIndex("dbo.LikeEntities", new[] { "User_UserId" });
            DropIndex("dbo.PermissionEntities", new[] { "UserEntity_UserId" });
            DropIndex("dbo.PermissionEntities", new[] { "Role_RoleId" });
            DropIndex("dbo.UserEntities", new[] { "Role_RoleId" });
            DropIndex("dbo.UserEntities", new[] { "UserEntity_UserId" });
            DropForeignKey("dbo.SubscriptionEntityUserEntities", "UserEntity_UserId", "dbo.UserEntities");
            DropForeignKey("dbo.SubscriptionEntityUserEntities", "SubscriptionEntity_SubscriptionId", "dbo.SubscriptionEntities");
            DropForeignKey("dbo.TagEntityEventEntities", "EventEntity_EventId", "dbo.EventEntities");
            DropForeignKey("dbo.TagEntityEventEntities", "TagEntity_TagId", "dbo.TagEntities");
            DropForeignKey("dbo.CityEntityCountryEntities", "CountryEntity_Countryid", "dbo.CountryEntities");
            DropForeignKey("dbo.CityEntityCountryEntities", "CityEntity_CityId", "dbo.CityEntities");
            DropForeignKey("dbo.LogEntities", "Action_ActionId", "dbo.ActionEntities");
            DropForeignKey("dbo.LogEntities", "User_UserId", "dbo.UserEntities");
            DropForeignKey("dbo.SessionEntities", "SessionId", "dbo.UserEntities");
            DropForeignKey("dbo.FollowerEntities", "Event_EventId", "dbo.EventEntities");
            DropForeignKey("dbo.FollowerEntities", "User_UserId", "dbo.UserEntities");
            DropForeignKey("dbo.CommentEntities", "EventEntity_EventId", "dbo.EventEntities");
            DropForeignKey("dbo.CommentEntities", "Creator_UserId", "dbo.UserEntities");
            DropForeignKey("dbo.EventEntities", "Creator_UserId", "dbo.UserEntities");
            DropForeignKey("dbo.EventEntities", "City_CityId", "dbo.CityEntities");
            DropForeignKey("dbo.EventEntities", "Country_Countryid", "dbo.CountryEntities");
            DropForeignKey("dbo.LikeEntities", "Event_EventId", "dbo.EventEntities");
            DropForeignKey("dbo.LikeEntities", "User_UserId", "dbo.UserEntities");
            DropForeignKey("dbo.PermissionEntities", "UserEntity_UserId", "dbo.UserEntities");
            DropForeignKey("dbo.PermissionEntities", "Role_RoleId", "dbo.RoleEntities");
            DropForeignKey("dbo.UserEntities", "Role_RoleId", "dbo.RoleEntities");
            DropForeignKey("dbo.UserEntities", "UserEntity_UserId", "dbo.UserEntities");
            DropTable("dbo.SubscriptionEntityUserEntities");
            DropTable("dbo.TagEntityEventEntities");
            DropTable("dbo.CityEntityCountryEntities");
            DropTable("dbo.ActionEntities");
            DropTable("dbo.LogEntities");
            DropTable("dbo.SessionEntities");
            DropTable("dbo.SubscriptionEntities");
            DropTable("dbo.FollowerEntities");
            DropTable("dbo.TagEntities");
            DropTable("dbo.CommentEntities");
            DropTable("dbo.CityEntities");
            DropTable("dbo.CountryEntities");
            DropTable("dbo.EventEntities");
            DropTable("dbo.LikeEntities");
            DropTable("dbo.PermissionEntities");
            DropTable("dbo.RoleEntities");
            DropTable("dbo.UserEntities");
        }
    }
}
